package com.chb.jblog.web.controller;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.chb.jblog.common.Cons;
import com.chb.jblog.entity.Links;
import com.chb.jblog.service.LinkService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("/layout")
public class Layout {
	private static final Log logger = LogFactory.getLog(Layout.class);
	@Autowired
	private LinkService linkService;
	@RequestMapping("/main")
	public String main(ModelMap map){
		map.put("title", "title");
		return "layout/main";
	}
	
	@RequestMapping("/main4")
	public String main4(){
		return "layout4/main";
	}
	@RequestMapping(value = {"/page/{pageNo}"})
	public String page(@PathVariable(value = "pageNo") Integer pageNo,ModelMap map){
		logger.info("pageNo:"+pageNo);
		if(pageNo==null||"".equals(pageNo)){
			pageNo = 1;
		}
		map.put("currentPage", pageNo);
		map.put("commonPageUrl", "");
		map.put("MaxPage", 23);
		return "page/pager";
	}
	@RequestMapping(value = {"/page"})
	public String pageFIrst(ModelMap map){
		return "redirect:/layout/page/1";
	}
	@RequestMapping(value={"/pageDb/{pageNo}"})
	public String pageDb(@PathVariable("pageNo") Integer pageNo,ModelMap map){
		logger.info("pageNo:"+pageNo);
		if(pageNo==null||"".equals(pageNo)){
			pageNo = 1;
		}
		PageHelper.startPage(pageNo, Cons.PAGE_SIZE);
		List<Links> links = linkService.selectByParameter(null);
		PageInfo<Links> pageInfo = new PageInfo<Links>(links);
		map.put("pageInfo", pageInfo);
		logger.info(JSON.toJSONString(pageInfo,SerializerFeature.WriteMapNullValue));
		return "page/pagerdb";
	}
	@RequestMapping(value = {"/pageDb"})
	public String pageDbFIrst(ModelMap map){
		return "forward:/layout/pageDb/1";
	}
}
